package com.liunian.subject.domain.handler.subject;

import com.liunian.subject.common.enums.IsDeletedFlagEnum;
import com.liunian.subject.common.enums.SubjectTypeEnum;
import com.liunian.subject.domain.entity.SubjectInfoBO;
import com.liunian.subject.domain.entity.SubjectOptionBO;
import com.liunian.subject.infra.basic.entity.SubjectMultiple;
import com.liunian.subject.infra.basic.entity.SubjectRadio;
import com.liunian.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class SubjectRadioHandler implements SubjectInfoTypeHandler{

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectTypeEnum getSubjectType() {
        return SubjectTypeEnum.RADIO;
    }

    @Override
    public Boolean add(SubjectInfoBO subjectInfoBO) {
        List<SubjectInfoBO.SubjectAnswerBO> option = subjectInfoBO.getOptionList();
        List<SubjectRadio> radioList = new ArrayList<>();
        option.forEach(data -> {
            SubjectRadio subjectRadio = new SubjectRadio();
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setOptionContent(data.getOptionContent());
            subjectRadio.setOptionType(data.getOptionType());
            subjectRadio.setIsCorrect(data.getIsCorrect());
            subjectRadio.setCreatedAt(LocalDateTime.now());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            radioList.add(subjectRadio);
        });
        return subjectRadioService.saveBatch(radioList);
    }

    @Override
    public SubjectOptionBO query(long subjectId) {
        List<SubjectRadio> subjectRadios = subjectRadioService.selectRadioAnswer(subjectId);
        List<SubjectInfoBO.SubjectAnswerBO> answerBOS = new ArrayList<>();
        subjectRadios.forEach(data -> {
            SubjectInfoBO.SubjectAnswerBO bo = new SubjectInfoBO.SubjectAnswerBO();
            bo.setOptionType(data.getOptionType());
            bo.setOptionContent(data.getOptionContent());
            bo.setIsCorrect(data.getIsCorrect());
            answerBOS.add(bo);
        });
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOption(answerBOS);
        return subjectOptionBO;
    }
}
